package com.sg.service.base.finance.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.finance.req.BatchQueryFinanceBillListByIdsReq;
import com.sg.entity.OmsBill;
import com.sg.service.base.finance.MOmsBillService;
import com.wicket.okrfinance.common.dto.FieldInfo;
import com.wicket.okrfinance.common.exception.BizException;
import com.wicket.okrfinance.common.tools.StringUtils;
import com.wicket.okrfinance.dal.mapper.mbg.OmsBillMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsBillServiceImpl
        extends ServiceImpl<OmsBillMapper, OmsBill>
        implements MOmsBillService {

    @Autowired
    OmsBillMapper omsBillMapper;

    /**
     * 7-1-02新增资金账单[7693]
     * gen by moon at 11/5/2023, 1:07:32 AM
     */
    @Trace(operationName = "7-1-02新增资金账单")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addFinanceBill(OmsBill omsBill) {
//     // TODO: 2024/1/19
        if (!StringUtils.isEmpty(omsBill.getOppositeLogo())) {
            omsBill.setOppositeLogo(CommonFunctionHelper.replaceFilePath(omsBill.getOppositeLogo()));
        }

        omsBill.setBillFlowId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsBill.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsBill.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsBill.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsBill.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsBill.setIsValid("FALSE");
        if (super.save(omsBill)) {
            return omsBill.getBillFlowId();
        } else {
            return "-1";
        }

    }

    /**
     * 7-1-02批量查询资金账单列表ByIds[8513]
     * gen by moon at 2/18/2024, 10:07:21 PM
     */
    @Trace(operationName = "7-1-02批量查询资金账单列表ByIds")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsBill> batchQueryFinanceBillListByIds(BatchQueryFinanceBillListByIdsReq reqDto) {

        try {
            QueryWrapper<OmsBill> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getFinanceBillList() == null || reqDto.getFinanceBillList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("bill_account_id", reqDto.getFinanceBillList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsBill.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsBill.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsBill.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsBill>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 7-1-02批量删资金账单[8514]
     * gen by moon at 2/18/2024, 10:07:24 PM
     */
    @Trace(operationName = "7-1-02批量删资金账单")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteFinanceBill(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }
}
